package com.annotation;

import org.junit.Test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @ClassName AnnotationDemo
 * @Description 注解
 * @Author 2214361854
 * @Date 2022/3/4 22:24
 * @Version 1.0
 */
//不在Target元注解的约束范围内
//@MyBook(name="《Java》",authors = {"编程"} ,price = 20)

@Book("/delete")
public class AnnotationDemo1 {

    @Book("/delete2")
    @MyBook(name="《Java》",authors = {"编程","开发"} ,price = 20)
    public void adds(){

    }

    @Test
    public void parsClass() throws Exception {
        Class c = AnnotationDemo1.class;
        Method method = c.getDeclaredMethod("adds");
        System.out.println(method);
        if (c.isAnnotationPresent(Book.class)) {
            Book book = (Book) c.getDeclaredAnnotation(Book.class);
            System.out.println(book.value());
        }
        if (method.isAnnotationPresent(MyBook.class)) {
            MyBook myBook = (MyBook) method.getDeclaredAnnotation(MyBook.class);
            System.out.println(myBook.name());
            System.out.println(Arrays.toString(myBook.authors()));
            System.out.println(myBook.price());
//            System.out.println(myBook);
        }
    }
}


/**
 * @ClassName MyBook
 * @Description 自定义注解、元注解
 * @Author 2214361854
 * @Date 2022/3/4 22:25
 * @Version 1.0
 */
//元注解，约束范围，生命周期
@Target({ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyBook{
    String name();
    String[] authors();
    double price();
}

/**
 * @author 2214361854
 */
@Retention(RetentionPolicy.RUNTIME)
@interface Book{
    String value();
}